import Vue from 'vue';
import base from './base';
import J_rule from './rule';
const { J_sys } = base;
const { START_I18N } = J_sys;
/*********************************************************************************
 * i18nPath为点前缀路径，tag定位到具体的属性上，两者共同构成完成的国际化路径
 * spare为备选值，即未开启国际化或尚未配置相关国际化时展现的值
 **********************************************************************************/
export const J_i18n = function(tag, spare = '', i18nPath) {
	if (START_I18N && Vue.i18n && tag) {
		//完整点路径
		let path = tag;
		//在可能的多个选项中找到可以成功匹配的路径
		if (Array.isArray(i18nPath)) i18nPath = i18nPath.find(path => (`${path}.${tag}` !== Vue.i18n.t(
			`${path}.${tag}`)));
		if (i18nPath) path = `${i18nPath}.${tag}`;
		let result = Vue.i18n.t(path);
		return result === path ? spare : result;
	} else {
		return spare
	}
}

export default {
	J_lanCode() {
		try {
			if (localStorage.lang) return localStorage.lang;
			let lang = navigator.language || navigator.userLanguage;
			if (lang === "zh-CN") {
				return 'zh'
			} else if (lang === "en-US") {
				return 'en'
			}
		} catch (e) {
			return console.log('J_lanCode非浏览器环境')
		}
	},
	J_ui() {
		try {
			return localStorage.storeUI || 'element';
		} catch (e) {
			return console.log('J_ui非浏览器环境')
		}
	},
	J_i18n,
	J_rule
}
